package com.psm.mobileController;

import com.google.gson.Gson;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.JsonRender;
import com.psm.model.*;

import org.apache.commons.lang.ArrayUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by cheam on 15/6/19.
 */
public abstract class BaseController extends Controller {

    protected String tableName;


    protected static IdGenerater idGenerater=new IdGenerater();


    /** 获取当前系统操作人ID */
    public Long getCurrentStudentId(){

        String login_user_id=getCookie("login_student_id");

        return idGenerater.to10(login_user_id);
    }



    @Override
    public void renderJson(Object object) {
        if (isIE()) {
            render(new JsonRender(object).forIE());
        }else {
            super.renderJson(object);
        }
    }

    @Override
    public void renderJson(String jsonText) {
        if (isIE()) {
            render(new JsonRender(jsonText).forIE());
        }else {
            super.renderJson(jsonText);
        }
    }

    @Override
    public void renderJson(String[] attrs) {
        if (isIE()) {
            render(new JsonRender(attrs).forIE());
        }else {
            super.renderJson(attrs);
        }
    }

    @Override
    public void renderJson() {
        if (isIE()) {
            render(new JsonRender().forIE());
        }else {
            super.renderJson();
        }
    }

    @Override
    public void renderJson(String key, Object value) {
        if (isIE()) {
            render(new JsonRender(key, value).forIE());
        }else {
            super.renderJson(key, value);
        }
    }

    /**
     * 判断是否万恶的IE
     * @return
     */
    public boolean isIE(){
        HttpServletRequest request = this.getRequest();
        String user_agent = request.getHeader("USER-AGENT");
        if (null == user_agent || "".equals(user_agent)) {
            return false;
        }
        String request_method = request.getMethod().toUpperCase();
        int msie = user_agent.toUpperCase().indexOf("MSIE");
        int ie11 = user_agent.toUpperCase().indexOf("RV");
        return (user_agent.toUpperCase().indexOf("MSIE")>0?true:false) || (user_agent.toUpperCase().indexOf("RV")>0?true:false) /*&& "POST".equals(request_method)*/;
    }


    public void sys_pa_code(){
        String table = getPara("table", "");
        String field = getPara("field", "");

        List<Record> list= Db.find("select * from " + SysPaCode.tableName + " where bus_table=? and bus_field=?", new Object[]{table, field});
        this.renderJson(list);
    }
}
